package com.itliang.web.controller.hardware;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itliang.annotation.Log;
import com.itliang.core.model.dto.equipment.EquipmentTypeQueryRequest;
import com.itliang.core.model.entity.equipment.EquipmentType;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.service.hardware.EquipmentTypeService;
import io.swagger.annotations.Api;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "设备类型管理接口")
@RestController
@RequestMapping("/admin/hardware/equipment/type")
public class EquipmentTypeController {

    @Resource
    private EquipmentTypeService equipmentTypeService;


    // region 增删改查

    /**
     * 创建设备类型
     *
     * @param equipmentType
     * @return
     */
    @Log(title = "设备类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public BaseResponse<Long> addEquipmentType(@RequestBody EquipmentType equipmentType) {
        equipmentTypeService.save(equipmentType);
        return ResultUtils.success(equipmentType.getId(), "恭喜，添加操作成功！");
    }

    /**
     * 删除设备类型
     *
     * @param id 设备类型id
     * @return
     */
    @Log(title = "设备类型管理", businessType = BusinessType.DELETE)
    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteEquipmentType(int id) {
        boolean b = equipmentTypeService.removeById(id);
        return ResultUtils.success(b, "恭喜，删除操作成功！");
    }

    /**
     * 批量删除设备类型
     *
     * @param idList id集合
     * @return
     */
    @Log(title = "设备类型管理", businessType = BusinessType.BATCHDELETE)
    @PostMapping("/delete/batch")
    public BaseResponse<Boolean> batchDeleteEquipmentType(@RequestBody List<Integer> idList) {
        boolean b = equipmentTypeService.removeBatchByIds(idList);
        return ResultUtils.success(b, "恭喜，批量删除操作成功！");
    }

    /**
     * 更新设备类型
     *
     * @param equipmentType 设备类型信息
     * @return
     */
    @Log(title = "设备类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateEquipmentType(@RequestBody EquipmentType equipmentType) {
        boolean result = equipmentTypeService.updateById(equipmentType);
        return ResultUtils.success(result, "恭喜，修改操作成功！");
    }

    /**
     * 根据 id 获取设备类型
     *
     * @param id 设备类型id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<EquipmentType> getEquipmentTypeById(Integer id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        EquipmentType equipmentType = equipmentTypeService.getById(id);
        return ResultUtils.success(equipmentType);
    }

    /**
     * 获取设备类型列表
     *
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<EquipmentType>> listEquipmentType() {
        List<EquipmentType> list = equipmentTypeService.list();
        return ResultUtils.success(list);
    }


    /**
     * 分页获取设备类型数据列表
     *
     * @param equipmentTypeQueryRequest 条件参数
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<EquipmentType>> listEquipmentTypeByPage(EquipmentTypeQueryRequest equipmentTypeQueryRequest) {
        long current = equipmentTypeQueryRequest.getCurrent();
        long size = equipmentTypeQueryRequest.getPageSize();
        String name = equipmentTypeQueryRequest.getName();
        String description = equipmentTypeQueryRequest.getDescription();
        QueryWrapper<EquipmentType> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), "name", name)
                .like(StringUtils.hasText(description), "description", description);
        Page<EquipmentType> page = equipmentTypeService.page(new Page<>(current, size), wrapper);
        return ResultUtils.success(page);
    }

    // endregion
}
